/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package upeu.cepre.business;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import upeu.cepre.model.Alumno;
import upeu.cepre.model.Examenes;

import upeu.cepre.util.DataBaseConexion;

/**
 *
 * @author Elvis Zanguetsu
 */
public class ExamenesManger {

    public Connection connection;

    public List<Examenes> Report() {
        List<Examenes> examenes = new ArrayList<Examenes>();


        try {

            connection = DataBaseConexion.getConexion();
            Statement statement = connection.createStatement();
            PreparedStatement prepareestatemente = connection.prepareStatement("SELECT * FROM  examenes  ");
            ResultSet rs = prepareestatemente.executeQuery();
            while (rs.next()) {
                Examenes examen = new Examenes();

                examen.setIdExamenes(rs.getString("idExamenes"));
                examen.setNumSemana(rs.getString("numSemana"));
                examen.setFechaExamen(rs.getString("fechaExamen"));
                examen.setIdPeriodo(rs.getString("idPeriodo"));


                examenes.add(examen);

            }
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return examenes;
    }

    public void Add(Examenes examenes) {
        try {
            connection = DataBaseConexion.getConexion();
            PreparedStatement preparedStatement =
                    connection.prepareStatement(
                    "INSERT INTO examenes ( idExamenes , numSemana , fechaExamen , idPeriodo ) VALUES (?,?,?,?)");

            preparedStatement.setString(1, examenes.getIdExamenes());
            preparedStatement.setString(2, examenes.getNumSemana());
            preparedStatement.setString(3, examenes.getFechaExamen());
            preparedStatement.setString(4, examenes.getIdPeriodo());
           
            preparedStatement.executeUpdate();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    public List<Examenes> findByIdPeriodo(String periodo) {
        List<Examenes> examenes = new ArrayList<Examenes>();


        try {

            connection = DataBaseConexion.getConexion();
            Statement statement = connection.createStatement();
            PreparedStatement prepareestatemente = connection.prepareStatement("SELECT * FROM  examenes WHERE idPeriodo=?");
            prepareestatemente.setString(1, periodo);

            ResultSet rs = prepareestatemente.executeQuery();
            while (rs.next()) {
                Examenes examen = new Examenes();

                examen.setIdExamenes(rs.getString("idExamenes"));
                examen.setNumSemana(rs.getString("numSemana"));
                examen.setFechaExamen(rs.getString("fechaExamen"));
                examen.setIdPeriodo(rs.getString("idPeriodo"));


                examenes.add(examen);

            }
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return examenes;
    }

    public String ObtenerId() {

        java.util.Date ahora = new java.util.Date();
        SimpleDateFormat formateador = new SimpleDateFormat("ddMMyyyy");
        ExamenesManger trabManager = new ExamenesManger();
        int a = trabManager.Report().size();
        return formateador.format(ahora) + "Examenes" + (a + 1);

    }
}
